package com.qfedu.edu.stockservice.queue.receive;

import com.qfedu.edu.stockservice.mapper.StockStorageMapper;
import com.qfedu.edu.stockservice.service.StockStorageService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author xiaobobo
 * @title: ReduceStockQueueMessageListener
 * @projectName seckill-fy-2401-parent
 * @description: TODO
 * @date 2024/11/14  10:13
 */
@Component
public class ReduceStockQueueMessageListener {

    @Autowired
    private StockStorageService stockStorageService;

    /**
     * 减库存的消息的监听
     * @param data
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {"stockQueue"})
    public void receive(byte[] data, Message message, Channel channel) throws IOException {
        //就是将数据弄成String类型
        String queueMessage = new String(data, "UTF-8");
        stockStorageService.reduce(queueMessage);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }

}
